var httpRequest = require('request');
const {
    Msg,
    Code,
    Constant
} = require('./consts');

//查询徒弟信息
exports.apiGetSonList = function (session, uid, pageNum) {
    return new Promise((resolve, reject) => {
        this.app.rpc.login.loginRemote.getUserSonList(session, uid, pageNum, function (err, users) {
            if (err) return reject(err);
            resolve(users);
        });
    });
}

//查询徒孙信息
exports.apiGetGrandSonList = function (session, uid, pageNum) {
    return new Promise((resolve, reject) => {
        this.app.rpc.login.loginRemote.getGrandSonList(session, uid, pageNum, function (err, users) {
            if (err) return reject(err);
            resolve(users);
        });
    });
}

//查询用户石油金钱信息
exports.apiGetUserMoneyAndOil = function (session, uid) {
    return new Promise((resolve, reject) => {
        this.app.rpc.login.loginRemote.userMoneyAndOil(session, uid, function (err, ret) {
            if (err) return reject(err);
            resolve(ret);
        });
    });
}

//获取累积提现金额
exports.apiGetTotalWithdraw = function (session, uid) {
    return new Promise((resolve, reject) => {
        this.app.rpc.login.loginRemote.getTotalWithdraw(session, uid, function (err, ret) {
            if (err) return reject(err);
            resolve(ret);
        });
    });
}

//查询全服务石油总量
exports.apiTotalOil = function (session) {
    return new Promise((resolve, reject) => {
        this.app.rpc.login.loginRemote.totalOil(session, function (err, ret) {
            if (err) return reject(err);
            resolve(ret);
        });
    });
}

//检查内存中的用户是否存在
exports.getUserInfo = function (session, uid) {
    return new Promise((resolve, reject) => {
        this.app.rpc.login.loginRemote.getUser(session, uid, function (err, userInfo) {
            if (err) return reject(err);
            resolve(userInfo);
        });
    });
}

/**
 * 获取父用户的信息
 * @param uid
 * @returns {Promise<unknown>}
 */
exports.apiGetParentInfo = async function (session, uid) {
    return new Promise((resolve, reject) => {
        this.app.rpc.login.loginRemote.getParentUser(session, uid, session.frontendId, function (err, userInfo) {
            if (err) return reject(err);
            resolve(userInfo);
        });
    });
}

/**
 * 获取用户二维码
 * @param uid
 * @returns {Promise<void>}
 */
exports.getQrCode = async function (uid) {
    httpRequest({
        url: 'http://oil.njtrtw.ltd/wechat/qrcode?id=' + uid,
        method: "GET"
    }, async function (error, response, body) {

        if (response.statusCode != 200) {
            console.error('getQrCode 执行失败 response.statusCOde' + response.statusCode)
            throw new Error(Msg.NET_WORK_BUSY);
        } else {
            var queryData = JSON.stringify({
                background: 'https://qiqiutree.leyougame.cc/oil_background.png',
                dress: [
                    {
                        type: 'image',
                        url: body, //二维码地址
                        xposition: 292,
                        yposition: 995,
                        gravity: "NorthWest",
                        ws: 0.22
                    }
                ]
            });

            await httpRequest({
                url: 'https://graphs.tsingmuth.com/graph/compound',
                method: "POST",
                json: true,
                headers: {
                    "content-type": "application/json",
                },
                body: queryData,
            }, function (error, response, body) {
                if (response.statusCode != 200) {
                    console.error('compoundPoster 执行失败 response.statusCode' + response.statusCode)
                    throw new Error(Msg.NET_WORK_BUSY);
                } else {
                    if (body.code != '000') {
                        throw new Error(Msg.NET_WORK_BUSY);
                    } else {
                        return body.data;
                    }
                }
            });
        }
    });
}

/**
 * 减少金钱
 * @param session
 * @param uid
 * @param money
 * @returns {Promise<unknown>}
 */
exports.apiReduceMoney = async function (session, uid, money, current_money) {
    return new Promise((resolve, reject) => {
        this.app.rpc.login.loginRemote.reduceMoney(session, uid, session.frontendId, money, current_money, function (err) {
            if (err) return reject(err);
            resolve();
        });
    });
}

/**
 * 微信打钱
 * @param uid
 * @param money
 * @returns {Promise<unknown>}
 */
exports.apiWithdrawMoney = async function (uid, money) {
    let self = this;
    return new Promise((resolve, reject) => {
        httpRequest.post(
            'http://oil.njtrtw.ltd/api/withdraw',
            {form: {'openid': uid, 'money': money}},
            function (error, response, body) {
                let json_data = JSON.parse(body);
                if (json_data.code != 200) {
                    console.error('playerWithdraw err: ' + json_data.message);
                    console.error('openid: ' + uid);
                    //回滚数据
                    self.app.userRemote(uid, "set", [uid, "user.first_withdraw", false]);
                    return reject(Msg.SERVER_BUSY);
                }
                resolve();
            }
        );
    });
}
